package com.mediacloud.app.newsmodule.activity

import android.graphics.Color
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.eventTrack.CDPTrackReportManager
import com.eventTrack.TXReportManager
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.navlive.NavLiveData
import com.mediacloud.app.newsmodule.utils.GridSpacingItemDecorationX
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.reslib.util.DataInvokeUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_more_normal_layout.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 纪录片更多界面
 * @property pageNum Int
 * @property moreAdapter MoreVideoCollectionAdapter?
 * @property catalog_id String?
 */
class MoreVideoCollectionListActivity : BaseBackActivity(), OnRefreshLoadMoreListener {

    var pageNum = 1
    var moreAdapter: MoreVideoCollectionAdapter?=null
    var catalog_id:String?=null



    override fun getLayoutResID(): Int {
        return R.layout.activity_more_normal_layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        catalog_id = intent.getStringExtra("id")?:""
        val title = intent.getStringExtra("title")?:""
        setTitle(title)
        loadingView = findViewById(R.id.mLoadingView)
        initStateView()
        showStateView(TYPE_LOADING, false)
        loadingImage.setOnClickListener {
            if (currentState != TYPE_LOADING) {
                showStateView(TYPE_LOADING, false)
                pageNum = 1
                getList()
            }
        }

        fresh.setOnRefreshLoadMoreListener(this)
        rvlist.layoutManager = GridLayoutManager(this,2)
        rvlist.addItemDecoration(GridSpacingItemDecorationX(2, resources.getDimensionPixelSize(R.dimen.dimen8), resources.getDimensionPixelSize(R.dimen.dimen16), false))
        moreAdapter = MoreVideoCollectionAdapter()
        rvlist.adapter = moreAdapter
        moreAdapter?.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as ArticleItem
            NewsItemClickUtils.OpenItemNewsHandle(
                view.context,
                item.type,
                item,
                CatalogItem()
            )
            if(TXReportManager.getInstance().firstTabInfo.tabName=="视听"){
                CDPTrackReportManager.getInstance().ShortVideoClick(item,position)
            }
        }
        getList()
    }


    private fun getList() {

        val params: MutableMap<String, String> = HashMap()
        params["pageNumber"] = "$pageNum"
        params["pageSize"] = "20"
        params["client"] = DataInvokeUtil.ClientFlag
        DataInvokeUtil.ziMeiTiApi.getContentList(catalog_id, params)
            .map {
                val sdf = SimpleDateFormat("yyyyMMdd", Locale.CHINA)
                val sdf2 = SimpleDateFormat("MM/dd", Locale.CHINA)
                val data = NavLiveData()
                data.readFromJson(it.body())
                if (data.state) {
                    data.weekList?.forEach {
                        it.date_format = sdf2.format(sdf.parse(it.date))
                    }
                }
                data
            }
            .onErrorReturn { null }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NavLiveData> {
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(data: NavLiveData) {
                    if (isFinishing || isDestroyed) {
                        return
                    }
                    closeStateView()
                    fresh.finishRefresh()
                    fresh.finishLoadMore()

                    if (pageNum == 1){
                        if (data.articleList.size > 0){
                            moreAdapter?.setNewData(data.articleList)
                        }else{
                            showStateView(TYPE_NO_CONTENT,false)
                        }
                    }else{
                        moreAdapter?.addData(data.articleList)
                    }

                }

                override fun onError(e: Throwable) {
                    showStateView(TYPE_NO_CONTENT, false)
                }

                override fun onComplete() {
                }
            })
    }


    override fun getStatusBarColor(): Int {
        return Color.WHITE
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageNum = 1
        getList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        pageNum++
        getList()
    }


    override fun getContent_show_top_color(): String {
        mTitileBar.setBackgroundColor(Color.WHITE)
        return "#333333"
    }
}